
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>email.encoders: 编码器 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="email.utils: 其他工具" href="email.utils.html" />
    <link rel="prev" title="email.charset: Representing character sets" href="email.charset.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/email.encoders.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="email.utils.html" title="email.utils: 其他工具"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="email.charset.html" title="email.charset: Representing character sets"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="netdata.html" >互联网数据处理</a> &#187;</li>
          <li class="nav-item nav-item-3"><a href="email.html" accesskey="U"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code> --- 电子邮件与 MIME 处理包</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-email.encoders">
<span id="email-encoders-encoders"></span><h1><a class="reference internal" href="#module-email.encoders" title="email.encoders: Encoders for email message payloads."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.encoders</span></code></a>: 编码器<a class="headerlink" href="#module-email.encoders" title="永久链接至标题">¶</a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/email/encoders.py">Lib/email/encoders.py</a></p>
<hr class="docutils" />
<p>此模块是旧版 (<code class="docutils literal notranslate"><span class="pre">Compat32</span></code>) email API 的组成部分。 在新版 API 中将由 <a class="reference internal" href="email.message.html#email.message.EmailMessage.set_content" title="email.message.EmailMessage.set_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_content()</span></code></a> 方法的 <em>cte</em> 形参提供该功能。</p>
<p>此模块在 Python 3 中已弃用。 这里提供的函数不应被显式地调用，因为 <a class="reference internal" href="email.mime.html#email.mime.text.MIMEText" title="email.mime.text.MIMEText"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEText</span></code></a> 类会在类实例化期间使用 <em>_subtype</em> 和 <em>_charset</em> 值来设置内容类型和 CTE 头。</p>
<p>本段落中的剩余文本是该模块的原始文档。</p>
<p>当创建全新的 <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> 对象时，你经常需要对载荷编码以便通过兼容的邮件服务器进行传输。 对于包含二进制数据的 <em class="mimetype">image/*</em> 和 <em class="mimetype">text/*</em> 类型的消息来说尤其如此。</p>
<p><a class="reference internal" href="email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> 包在其 <code class="xref py py-mod docutils literal notranslate"><span class="pre">encoders</span></code> 模块中提供了一些方便的编码。 这些编码器实际上由 <a class="reference internal" href="email.mime.html#email.mime.audio.MIMEAudio" title="email.mime.audio.MIMEAudio"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEAudio</span></code></a> 和 <a class="reference internal" href="email.mime.html#email.mime.image.MIMEImage" title="email.mime.image.MIMEImage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEImage</span></code></a> 类构造函数使用，以提供默认编码。 所有编码器函数只接受一个参数，即要编码的消息对象。 它们通常提取有效数据，对其进行编码，并将有效数据重置为此新编码的值。 他们还应该根据需要设置 <em class="mailheader">Content-Transfer-Encoding</em> 标头。</p>
<p>请注意，这些函数对于多段消息没有意义。 它们必须应用到各个单独的段上面，而不是整体。如果直接传递一个多段类型的消息，会产生一个 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> 错误。</p>
<p>下面是提供的编码函数：</p>
<dl class="function">
<dt id="email.encoders.encode_quopri">
<code class="sig-prename descclassname">email.encoders.</code><code class="sig-name descname">encode_quopri</code><span class="sig-paren">(</span><em class="sig-param">msg</em><span class="sig-paren">)</span><a class="headerlink" href="#email.encoders.encode_quopri" title="永久链接至目标">¶</a></dt>
<dd><p>将有效数据编码为quoted-printable形式，并将:mailheader:<cite>Content-Transfer-Encoding`标头设置为``quoted-printable`</cite> <a class="footnote-reference brackets" href="#id2" id="id1">1</a>. 。当大多数实际的数据是普通的可打印数据但包含少量不可打印的字符时，这是一个很好的编码。</p>
</dd></dl>

<dl class="function">
<dt id="email.encoders.encode_base64">
<code class="sig-prename descclassname">email.encoders.</code><code class="sig-name descname">encode_base64</code><span class="sig-paren">(</span><em class="sig-param">msg</em><span class="sig-paren">)</span><a class="headerlink" href="#email.encoders.encode_base64" title="永久链接至目标">¶</a></dt>
<dd><p>将有效载荷编码为 base64 形式，并将 <em class="mailheader">Content-Transfer-Encoding</em> 标头设为 <code class="docutils literal notranslate"><span class="pre">base64</span></code>。 当你的载荷主要包含不可打印数据时这是一种很好用的编码格式，因为它比 quoted-printable 更紧凑。 base64 编码格式的缺点是它会使文本变成人类不可读的形式。</p>
</dd></dl>

<dl class="function">
<dt id="email.encoders.encode_7or8bit">
<code class="sig-prename descclassname">email.encoders.</code><code class="sig-name descname">encode_7or8bit</code><span class="sig-paren">(</span><em class="sig-param">msg</em><span class="sig-paren">)</span><a class="headerlink" href="#email.encoders.encode_7or8bit" title="永久链接至目标">¶</a></dt>
<dd><p>这并不实际改变消息的有效载荷，但它会基于载荷数据将 <em class="mailheader">Content-Transfer-Encoding</em> 标头相应地设为 <code class="docutils literal notranslate"><span class="pre">7bit</span></code> 或 <code class="docutils literal notranslate"><span class="pre">8bit</span></code>。</p>
</dd></dl>

<dl class="function">
<dt id="email.encoders.encode_noop">
<code class="sig-prename descclassname">email.encoders.</code><code class="sig-name descname">encode_noop</code><span class="sig-paren">(</span><em class="sig-param">msg</em><span class="sig-paren">)</span><a class="headerlink" href="#email.encoders.encode_noop" title="永久链接至目标">¶</a></dt>
<dd><p>这样什么都不会做；它甚至不会设置 <em class="mailheader">Content-Transfer-Encoding</em> 标头。</p>
</dd></dl>

<p class="rubric">备注</p>
<dl class="footnote brackets">
<dt class="label" id="id2"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
<dd><p>请注意使用 <a class="reference internal" href="#email.encoders.encode_quopri" title="email.encoders.encode_quopri"><code class="xref py py-meth docutils literal notranslate"><span class="pre">encode_quopri()</span></code></a> 编码格式还会对数据中的所有制表符和空格符进行编码。</p>
</dd>
</dl>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="email.charset.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.charset</span></code>: Representing character sets</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="email.utils.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.utils</span></code>: 其他工具</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/email.encoders.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="email.utils.html" title="email.utils: 其他工具"
             >下一页</a> |</li>
        <li class="right" >
          <a href="email.charset.html" title="email.charset: Representing character sets"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="netdata.html" >互联网数据处理</a> &#187;</li>
          <li class="nav-item nav-item-3"><a href="email.html" ><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code> --- 电子邮件与 MIME 处理包</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>